/**
 * 
 */
package ken.app.xmlhandler;

import java.util.ArrayList;
import java.util.List;

import ken.app.bean.SportBabyListBean;
import ken.app.bean.TreeAllBean;


import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import android.util.Log;

/**
 * @author ken.chung
 * 
 */
public class SportBabyListXMLHandler extends DefaultHandler {
	
	/* 變數宣告 */
	private boolean in_item = false;
	private boolean in_baby_id = false;
	private boolean in_baby_name = false;
	private boolean in_baby_score = false;
	private boolean in_baby_percent = false;
	private boolean in_baby_sec = false;
	private boolean in_pic1 = false;
	private boolean in_pic2 = false;
	private boolean in_pic3 = false;
	private boolean in_baby1 = false;
	private boolean in_baby2 = false;
	private boolean in_camera1 = false;
	private boolean in_camera2 = false;
	private boolean in_camera3 = false;
	private boolean in_camera4 = false;
	private boolean in_camera5 = false;
	private boolean in_camera6 = false;
	private boolean in_camera7 = false;
	private boolean in_camera8 = false;
	private boolean in_camera9 = false;
	private boolean in_camera10 = false;
	private boolean in_camera_bg = false;
	private boolean in_lastmodify = false;
	
	private boolean in_result = false;
	
	private List<SportBabyListBean> list;
	private SportBabyListBean mSportBabyListBean;
	private String title = "";
	private StringBuffer buf = new StringBuffer();

	/* 將轉換成List<News>的XML資料回傳 */
	public List<SportBabyListBean> getParsedData() {
		return list;
	}

	/* 將解析出的RSS title回傳 */
	public String getRssTitle() {
		return title;
	}

	/* XML文件開始解析時呼叫此method */
	@Override
	public void startDocument() throws SAXException {
		if (list == null) {
			list = new ArrayList<SportBabyListBean>();
		}
	}

	/* XML文件結束解析時呼叫此method */
	@Override
	public void endDocument() throws SAXException {
	}

	/* 解析到Element的開頭時呼叫此method */
	@Override
	public void startElement(String namespaceURI, String localName,
			String qName, Attributes atts) throws SAXException {
		
//		if (localName.equals("result")) {
//			if (buf.toString().trim() == "1") {
//				in_result = true;
//				buf.setLength(0);
//			}
//
//		}
//		
//		if (in_result == true) {
			if (localName.equals("item")) {
				this.in_item = true;
				/* 解析到item的開頭時new一個News物件 */
				mSportBabyListBean = new SportBabyListBean();
				Log.d("ken", buf.toString());
			} else if (localName.equals("baby_id")) {
				if (this.in_item) {
					this.in_baby_id = true;
				} 
			} else if (localName.equals("name")) {
				if (this.in_item) {
					this.in_baby_name = true;
				}
			} else if (localName.equals("score")) {
				if (this.in_item) {
					this.in_baby_score = true;
				}
			} else if (localName.equals("percent")) {
				if (this.in_item) {
					this.in_baby_percent = true;
				}
			}	else if (localName.equals("sec")) {
				if (this.in_item) {
					this.in_baby_sec = true;
				}
			}	else if (localName.equals("pic1")) {
				if (this.in_item) {
					this.in_pic1 = true;
				}
			}  else if (localName.equals("pic2")) {
				if (this.in_item) {
					this.in_pic2 = true;
				}
			}  else if (localName.equals("pic3")) {
				if (this.in_item) {
					this.in_pic3 = true;
				}
			}  else if (localName.equals("baby1")) {
				if (this.in_item) {
					this.in_baby1 = true;
				}
			}  else if (localName.equals("baby2")) {
				if (this.in_item) {
					this.in_baby2 = true;
				}
			}  else if (localName.equals("camera1")) {
				if (this.in_item) {
					this.in_camera1 = true;
				}
			}  else if (localName.equals("camera2")) {
				if (this.in_item) {
					this.in_camera2 = true;
				}
			}  else if (localName.equals("camera3")) {
				if (this.in_item) {
					this.in_camera3 = true;
				}
			}  else if (localName.equals("camera4")) {
				if (this.in_item) {
					this.in_camera4 = true;
				}
			}  else if (localName.equals("camera5")) {
				if (this.in_item) {
					this.in_camera5 = true;
				}
			}  else if (localName.equals("camera6")) {
				if (this.in_item) {
					this.in_camera6 = true;
				}
			}  else if (localName.equals("camera7")) {
				if (this.in_item) {
					this.in_camera7 = true;
				}
			}  else if (localName.equals("camera8")) {
				if (this.in_item) {
					this.in_camera8 = true;
				}
			}  else if (localName.equals("camera9")) {
				if (this.in_item) {
					this.in_camera9 = true;
				}
			}  else if (localName.equals("camera10")) {
				if (this.in_item) {
					this.in_camera10 = true;
				}
			}  else if (localName.equals("camera_bg")) {
				if (this.in_item) {
					this.in_camera_bg = true;
				}
			}  else if (localName.equals("lastmodify")) {
				if (this.in_item) {
					this.in_lastmodify = true;
				}
			}
//		}
		
		buf.setLength(0);
	}

	/* 解析到Element的結尾時呼叫此method */
	@Override
	public void endElement(String namespaceURI, String localName, String qName)
			throws SAXException {
		if (localName.equals("item")) {
			this.in_item = false;
			/* 解析到item的結尾時將News物件寫入List中 */
			list.add(mSportBabyListBean);
		} else if (localName.equals("baby_id")) {
			if (this.in_item) {
				/* 設定News物件的title */
				mSportBabyListBean.setBabyId(Integer.valueOf(buf.toString().trim()));
				buf.setLength(0);
				this.in_baby_id = false;
			} 
		} else if (localName.equals("name")) {
			if (this.in_item) {
				/* 設定News物件的link */
				mSportBabyListBean.setBabyName(buf.toString().trim());
				buf.setLength(0);
				this.in_baby_name = false;
			}
		}else if (localName.equals("score")) {
			if (in_item) {
				/* 設定News物件的pubDate */
				mSportBabyListBean.setBabyScore(Integer.valueOf(buf.toString().trim()));
				buf.setLength(0);
				this.in_baby_score = false;
			}
		} else if (localName.equals("percent")) {
			if (in_item) {
				/* 設定News物件的pubDate */
				mSportBabyListBean.setBabyPercent(Integer.valueOf(buf.toString().trim()));
				buf.setLength(0);
				this.in_baby_score = false;
			}
		} else if (localName.equals("sec")) {
			if (in_item) {
				/* 設定News物件的pubDate */
				mSportBabyListBean.setBabySec(Integer.valueOf(buf.toString().trim()));
				buf.setLength(0);
				this.in_baby_sec = false;
			}
		}else if (localName.equals("pic1")) {
			if (in_item) {
				/* 設定News物件的pubDate */
				mSportBabyListBean.setPic1(buf.toString().trim());
				buf.setLength(0);
				this.in_pic1 = false;
			}
		} else if (localName.equals("pic2")) {
			if (in_item) {
				/* 設定News物件的pubDate */
				mSportBabyListBean.setPic2(buf.toString().trim());
				buf.setLength(0);
				this.in_pic2 = false;
			}
		} else if (localName.equals("pic3")) {
			if (in_item) {
				/* 設定News物件的pubDate */
				mSportBabyListBean.setPic3(buf.toString().trim());
				buf.setLength(0);
				this.in_pic2 = false;
			}
		} else if (localName.equals("baby1")) {
			if (in_item) {
				/* 設定News物件的pubDate */
				mSportBabyListBean.setBaby1(buf.toString().trim());
				mSportBabyListBean.addCamerasToList(buf.toString().trim());
				buf.setLength(0);
				this.in_baby1 = false;
			}
		} else if (localName.equals("baby2")) {
			if (in_item) {
				/* 設定News物件的pubDate */
				mSportBabyListBean.setBaby2(buf.toString().trim());
				mSportBabyListBean.addCamerasToList(buf.toString().trim());
				buf.setLength(0);
				this.in_baby2 = false;
			}
		} else if (localName.equals("camera1")) {
			if (in_item) {
				/* 設定News物件的pubDate */
				mSportBabyListBean.setCamera1(buf.toString().trim());
				mSportBabyListBean.addCamerasToList(buf.toString().trim());
				buf.setLength(0);
				this.in_camera1 = false;
			}
		} else if (localName.equals("camera2")) {
			if (in_item) {
				/* 設定News物件的pubDate */
				mSportBabyListBean.setCamera2(buf.toString().trim());
				mSportBabyListBean.addCamerasToList(buf.toString().trim());
				buf.setLength(0);
				this.in_camera2 = false;
			}
		} else if (localName.equals("camera3")) {
			if (in_item) {
				/* 設定News物件的pubDate */
				mSportBabyListBean.setCamera3(buf.toString().trim());
				mSportBabyListBean.addCamerasToList(buf.toString().trim());
				buf.setLength(0);
				this.in_camera3 = false;
			}
		} else if (localName.equals("camera4")) {
			if (in_item) {
				/* 設定News物件的pubDate */
				mSportBabyListBean.setCamera4(buf.toString().trim());
				mSportBabyListBean.addCamerasToList(buf.toString().trim());
				buf.setLength(0);
				this.in_camera4 = false;
			}
		} else if (localName.equals("camera5")) {
			if (in_item) {
				/* 設定News物件的pubDate */
				mSportBabyListBean.setCamera5(buf.toString().trim());
				mSportBabyListBean.addCamerasToList(buf.toString().trim());
				buf.setLength(0);
				this.in_camera5 = false;
			}
		} else if (localName.equals("camera6")) {
			if (in_item) {
				/* 設定News物件的pubDate */
				mSportBabyListBean.setCamera6(buf.toString().trim());
				mSportBabyListBean.addCamerasToList(buf.toString().trim());
				buf.setLength(0);
				this.in_camera6 = false;
			}
		} else if (localName.equals("camera7")) {
			if (in_item) {
				/* 設定News物件的pubDate */
				mSportBabyListBean.setCamera7(buf.toString().trim());
				mSportBabyListBean.addCamerasToList(buf.toString().trim());
				buf.setLength(0);
				this.in_camera7 = false;
			}
		} else if (localName.equals("camera8")) {
			if (in_item) {
				/* 設定News物件的pubDate */
				mSportBabyListBean.setCamera8(buf.toString().trim());
				mSportBabyListBean.addCamerasToList(buf.toString().trim());
				buf.setLength(0);
				this.in_camera8 = false;
			}
		} else if (localName.equals("camera9")) {
			if (in_item) {
				/* 設定News物件的pubDate */
				mSportBabyListBean.setCamera9(buf.toString().trim());
				mSportBabyListBean.addCamerasToList(buf.toString().trim());
				buf.setLength(0);
				this.in_camera9 = false;
			}
		} else if (localName.equals("camera10")) {
			if (in_item) {
				/* 設定News物件的pubDate */
				mSportBabyListBean.setCamera10(buf.toString().trim());
				mSportBabyListBean.addCamerasToList(buf.toString().trim());
				buf.setLength(0);
				this.in_camera10 = false;
			}
		} else if (localName.equals("camera_bg")) {
			if (in_item) {
				/* 設定News物件的pubDate */
				mSportBabyListBean.setCameraBG(buf.toString().trim());
				if (!(mSportBabyListBean.getCameraBG().equals(""))) {
					mSportBabyListBean.addCamerasToList(buf.toString().trim());
				}
				buf.setLength(0);
				this.in_camera_bg = false;
			}
		} else if (localName.equals("lastmodify")) {
			if (in_item) {
				/* 設定News物件的pubDate */
				mSportBabyListBean.setLastmodify(Long.valueOf(buf.toString().trim()));
				buf.setLength(0);
				this.in_lastmodify = false;
			}
		}
	}

	/* 取得Element的開頭結尾中間夾的字串 */
	@Override
	public void characters(char ch[], int start, int length) {
		if (this.in_item || this.in_lastmodify) {
			/* 將char[]加入StringBuffer */
			buf.append(ch, start, length);
		}
	}
}